#include "StdAfx.h"
#include "ServiceInvoker.h"
#include "soapbdcSoapBindingProxy.h"
#include <tchar.h>
#include <atlconv.h>

using namespace bdc::service;

bdcSoapBindingProxy proxy;

LPWSTR heapStr(LPCWSTR pStrS)
{
	const int tagLen = wcslen(pStrS);
	LPWSTR pStrD = new WCHAR[tagLen];
	wcscpy(pStrD, pStrS);
	return pStrD;
}

ServiceInvoker::ServiceInvoker(void)
{
}

ServiceInvoker::~ServiceInvoker(void)
{
}

BOOL ServiceInvoker::getTagList(vector<TagInfo*>& result)
{
	USES_CONVERSION;

	ns1__getTagList param;
	ns1__getTagListResponse response;

	if(proxy.getTagList(&param, &response) == SOAP_OK) {
		vector<ns1__tagInfo*> tagList = response.return_;

		for(int i = 0; i < tagList.size(); i++) {
			TagInfo* tagInfo = new TagInfo;

			LPCWSTR pTag = A2CW(tagList[i]->tag->c_str());

			tagInfo->tagId = tagList[i]->tagId;
			tagInfo->userId = tagList[i]->userId;
			tagInfo->tag = heapStr(pTag);
			tagInfo->amount = tagList[i]->amount;

			result.push_back(tagInfo);
		}

		return TRUE;
	}
	else {
		return FALSE;
	}
}

BOOL ServiceInvoker::getTagRecordList(LONG64 tagId, vector<RecordInfo*>& result)
{
	USES_CONVERSION;

	ns1__getTagRecordList param;
	ns1__getTagRecordListResponse response;

	param.tagId = tagId;
	param.from = 0;
	param.to = 100;

	if(proxy.getTagRecordList(&param, &response) == SOAP_OK) {
		vector<ns1__recordInfo*> recordList = response.return_;

		for(int i = 0; i < recordList.size(); i++) {
			RecordInfo* recordInfo = new RecordInfo;

			LPCWSTR pMemory = A2CW(recordList[i]->memory->c_str());
			LPCWSTR pDescription = A2CW(recordList[i]->description->c_str());;
			LPCWSTR pTag = A2CW(recordList[i]->tag->c_str());

			recordInfo->recordId = recordList[i]->recordId;
			recordInfo->userId = recordList[i]->userId;
			recordInfo->memory = heapStr(pMemory);
			recordInfo->description = heapStr(pDescription);
			recordInfo->tag = heapStr(pTag);
			recordInfo->addTime = recordList[i]->addTime;

			result.push_back(recordInfo);
		}

		return TRUE;
	}
	else {
		return FALSE;
	}
}

BOOL ServiceInvoker::getTodayRecordList(vector<RecordInfo*>& result)
{
	USES_CONVERSION;

	ns1__getTodayRecordList param;
	ns1__getTodayRecordListResponse response;

	if(proxy.getTodayRecordList(&param, &response) == SOAP_OK) {
		vector<ns1__recordInfo*> recordList = response.return_;

		for(int i = 0; i < recordList.size(); i++) {
			RecordInfo* recordInfo = new RecordInfo;

			LPCWSTR pMemory = A2CW(recordList[i]->memory->c_str());
			LPCWSTR pDescription = A2CW(recordList[i]->description->c_str());;
			LPCWSTR pTag = A2CW(recordList[i]->tag->c_str());

			recordInfo->recordId = recordList[i]->recordId;
			recordInfo->userId = recordList[i]->userId;
			recordInfo->memory = heapStr(pMemory);
			recordInfo->description = heapStr(pDescription);
			recordInfo->tag = heapStr(pTag);
			recordInfo->addTime = recordList[i]->addTime;

			result.push_back(recordInfo);
		}

		return TRUE;
	}
	else {
		return FALSE;
	}
}

BOOL ServiceInvoker::addRecord(LPCWSTR pMemory, LPCWSTR pDescription, LPCWSTR pTag)
{
	USES_CONVERSION;

	ns1__addRecord param;
	ns1__addRecordResponse response;

	string mstring(W2CA(pMemory));
	string dstring(W2CA(pDescription));
	string tstring(W2CA(pTag));

	param.memory = &mstring;
	param.description = &dstring;
	param.tag = &tstring;

	if(proxy.addRecord(&param, &response) == SOAP_OK) {
		return response.return_;
	}
	else {
		return FALSE;
	}
}

BOOL ServiceInvoker::updateRecord(LONG64 recordId, LPCWSTR pMemory, LPCWSTR pDescription, LPCWSTR pTag)
{
	USES_CONVERSION;

	ns1__updateRecord param;
	ns1__updateRecordResponse response;

	string mstring(W2CA(pMemory));
	string dstring(W2CA(pDescription));
	string tstring(W2CA(pTag));

	param.recordId = recordId;
	param.memory = &mstring;
	param.description = &dstring;
	param.tag = &tstring;

	if(proxy.updateRecord(&param, &response) == SOAP_OK) {
		return response.return_;
	}
	else {
		return FALSE;
	}
}

BOOL ServiceInvoker::delRecord(LONG64 recordId)
{
	ns1__delRecord param;
	ns1__delRecordResponse response;

	param.recordId = recordId;

	if(proxy.delRecord(&param, &response) == SOAP_OK) {
		return response.return_;
	}
	else {
		return FALSE;
	}
}

BOOL ServiceInvoker::updateTag(LONG64 tagId, LPCWSTR tagName)
{
	USES_CONVERSION;

	ns1__updateTag param;
	ns1__updateTagResponse response;

	string tag(W2CA(tagName));

	param.tagId = tagId;
	param.tag = &tag;

	if(proxy.updateTag(&param, &response) == SOAP_OK) {
		return response.return_;
	}
	else {
		return FALSE;
	}
}

BOOL ServiceInvoker::delTag(LONG64 tagId)
{
	ns1__delTag param;
	ns1__delTagResponse response;

	param.tagId = tagId;

	if(proxy.delTag(&param, &response) == SOAP_OK) {
		return response.return_;
	}
	else {
		return FALSE;
	}
}

LONG64 ServiceInvoker::login(LPCWSTR mail, LPCWSTR password)
{
	USES_CONVERSION;

	ns1__login param;
	ns1__loginResponse response;

	string mstring(W2CA(mail));
	string pstring(W2CA(password));

	param.mail = &mstring;
	param.password = &pstring;

	if(proxy.login(&param, &response) == SOAP_OK) {
		return response.return_;
	}
	else {
		return -2;
	}
}
